//
//  ZyaAddressFormVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/8/24.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit

class ZyaAddressFormVC: UIViewController,refreshable {
    
    var model = ZyaClientAddress(JSON:["":""])! {
        didSet {
            self.nameItem.setVal(model.kLinkName.unwrap)
            self.telItem.setVal(model.kLinkPhone.unwrap)
            self.proItem.setVal(model.kProvince.unwrap + model.kCity.unwrap + model.kArea.unwrap)
            self.addrItem.setVal(model.clientAdd.unwrap)
            self.defaultItem.setSwitchOn(model.currMark == 1 ? true : false)
        }
    }
    var currentMark = 0
    var idKey = ""
    
    var bg :UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5
        return v
    }()
    
    var nameItem :KSFormHItem = {
        let v = KSFormHItem(title: "联系人")
        return v
    }()
    var telItem :KSFormHItem = {
        let appe = KSFormHAppear()
            .phoneInput(true)
        let v = KSFormHItem(title: "手机号码",appearance: appe)
        return v
    }()
    var proItem :KSFormHItem = {
        let appe = KSFormHAppear()
            .placeholder("省、市、区")
        let v = KSFormHItem(title: "选择地区",appearance:appe)
        return v
    }()
    
    var addrItem :KSFormHItem = {
        let v = KSFormHItem(title: "详细地址", type: .tempTextView)
        return v
    }()
    var defaultItem :KSSwitchItem = {
        let v = KSSwitchItem(title: "默认地址")
        return v
    }()

    var typeX:ZyaCAddrType!
    init(type:ZyaCAddrType){
        super.init(nibName: nil, bundle: nil)
        self.typeX = type
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.nameItem.textValueChangeClo = { str in
            self.model.kLinkName = str
        }
        self.telItem.textValueChangeClo = { str in
            self.model.kLinkPhone = str
        }
        self.proItem.actionClosure = {
            let vc = ProvinceCityController(selectType: .proCityDict)
            vc.tripleSelectClosure = { province,city,district in
                
                self.model.kProvince = province
                self.model.kCity = city
                self.model.kArea = district
                self.proItem.setVal(self.model.kProvince.unwrap + self.model.kCity.unwrap + self.model.kArea.unwrap)
                
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
        self.addrItem.textValueChangeClo = { str in
            self.model.clientAdd = str
        }
        self.defaultItem.switchValueChangeClo = { boolx in
            self.model.currMark = boolx ? 1 : 0
        }
        
        edgesForExtendedLayout = UIRectEdge()
        view.backgroundColor = Theme.awModuleBgColor
         view.addSubview(selectButton)
        view.addSubview(submitButton)
        view.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(6)
            make.right.equalTo(self.view).offset(-6)
            make.top.equalTo(self.view).offset(10)
        }
        bg.addSubview(nameItem)
        nameItem.snp.makeConstraints { make in
            make.left.right.top.equalTo(bg)
            make.height.equalTo(43)
        }
        bg.addSubview(telItem)
        telItem.snp.makeConstraints { make in
            make.left.right.equalTo(bg)
            make.top.equalTo(nameItem.snp.bottom)
            make.height.equalTo(43)
        }
        bg.addSubview(proItem)
        proItem.snp.makeConstraints { make in
            make.left.right.equalTo(bg)
            make.top.equalTo(telItem.snp.bottom)
            make.height.equalTo(43)
        }
        bg.addSubview(addrItem)
        addrItem.snp.makeConstraints { make in
            make.left.right.equalTo(bg)
            make.top.equalTo(proItem.snp.bottom)
            make.height.equalTo(86)
        }
        bg.addSubview(defaultItem)
        defaultItem.snp.makeConstraints { make in
            make.left.right.equalTo(bg)
            make.top.equalTo(addrItem.snp.bottom)
            make.height.equalTo(43)
            make.bottom.equalTo(bg)
        }
         submitButton.addTarget(self, action: #selector(submitAction), for: .touchUpInside)
      
       
        submitButton.snp.makeConstraints { (maker) in
            maker.leading.equalTo(8)
            maker.trailing.equalToSuperview().inset(8)
            maker.top.equalTo(bg.snp.bottom).offset(24)
            maker.height.equalTo(44)
        }
    }
   
    @objc private func submitAction() {
        
        view.endEditing(true)
        
        let vali = Validation()
         vali.required(value: self.model.kLinkName, msg: "联系人不为空")
        vali.required(value: self.model.kLinkPhone, msg: "联系电话不为空")
        vali.required(value: self.model.clientAdd, msg: "详细地址不为空")

        guard vali.validate() else {
            return
        }
    
        model.idKey = idKey
        model.clientId = USERINFO.kfId!
        model.clientName = USERINFO.kfName!
        
        model.kAddrType = self.typeX.rawValue
        KSNetwork.shared.doRequest(API.addClientAddress(data: dictionaryToJson(model.dictionaryRepresentation()), idKey: idKey)) { (data: BaseResult<OperateResult>) in
            if data.message.unwrap.contains("成功") {
                if let clo = self.refreshClosure {
                    clo()
                }
                 self.navigationController?.popViewController(animated: true)
                
            }
        }
    }
     
    lazy var selectButton: UIButton = {
        let button = UIButton()
        button.setTitle("设为默认", for: UIControl.State())
        button.setTitleColor(UIColor.black, for: UIControl.State())
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.setImage(#imageLiteral(resourceName: "s_unselect"), for: UIControl.State())
        return button
    }()
    
    lazy var submitButton: UIButton = {
        let button = UIButton()
        button.layer.cornerRadius = 5
        button.backgroundColor = Theme.primaryColor()
        button.setTitle("保存", for: UIControl.State())
        return button
    }()
}
